package ext.opensource.netty.client.core;

public class LoginCheckThread extends Thread {
	private final BaseClient client;
	private final long timeOutMillis;
	private final long startTime;

	public LoginCheckThread(final BaseClient client, final long startTime, final long timeOutMillis) {
		this.client = client;
		this.timeOutMillis = timeOutMillis;
		this.startTime = startTime;
	}

	@Override
	public void run() {
		check();
	}

	public void check() {
		boolean bTimeOut = false;
		while (true) {
			try {
				Thread.sleep(500);
			} catch (final InterruptedException e) {
				break;
			}

			if (timeOutMillis > 0) {
				if ((System.currentTimeMillis() - startTime) > timeOutMillis) {
					bTimeOut = true;
				}
			}

			if ((this.client == null)
					|| (this.client.checkConnectFlag(bTimeOut, System.currentTimeMillis() - startTime))) {
				break;
			}
			if (bTimeOut) {
				break;
			}
		}
	}
}
